package spring.chatroom.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import spring.chatroom.java_chatroom.pojo.response.response;

@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {
    private final View error;

    public ExceptionAdvice(View error) {
        this.error = error;
    }

    @ExceptionHandler(NumberFormatException.class)
    public response handleNumberFormatException(NumberFormatException e) {
        log.error("参数格式不对：{}", e.getMessage());
        return response.fail("请求的参数错误");
    }

    @ExceptionHandler(NoResourceFoundException.class)
    public response handleNoResourceFoundException(NoResourceFoundException e) {
        log.info(e.getMessage());
        return response.fail("");
    }
    @ExceptionHandler(Exception.class)
    public response handleException(Exception e) {

        log.error("发生了异常 {}", e.getMessage());
        e.printStackTrace();
        return response.fail("后端发生异常");
    }
}
